import java.awt.Polygon;
import java.awt.geom.Point2D;
import java.util.Vector;


public class Cone extends OrientedShape{
	private double radius;
	private double angle;
	
	
	public Cone(double r, double a){
		super();
		radius = r;
		angle = a;
	}
	
	public double getRadius() {
		return radius;
	}
	
	public void setRadius(double radius) {
		this.radius = radius;
	}
	
	public double getAngle() {
		return angle;
	}
	
	public void setAngle(double angle) {
		this.angle = angle;
	}
	
	public double getStartAngle(){
		return Math.atan2(direction.getY2(), direction.getX2())*180/Math.PI - angle/2;
	}
	
	public Point2D.Double getP1(){
		double alpha = getStartAngle()*Math.PI/180;
		Point2D.Double p1 = new Point2D.Double(origin.x + Math.cos(alpha)*radius, origin.y + Math.sin(alpha)* radius);
		return p1;
	}
	
	public Point2D.Double getP2(){
		double beta = (getStartAngle() + angle)*Math.PI/180;
		Point2D.Double p2 = new Point2D.Double(origin.x + Math.cos(beta)*radius, origin.y + Math.sin(beta)* radius);
		return p2;
	}
	
	public Vector<Line> getLineDistribution(double granularity){
		Vector<Line> res = new Vector<Line>();
		double angleRad = angle * Math.PI / 180;
		double startAngleRad = getStartAngle() * Math.PI / 180;
		
		int n = (int) Math.ceil(angleRad * radius / granularity); // <-
		double delta = angleRad / n;
		
		for (int i = 0; i <= n ; i++){
			Line line = new Line(radius, granularity);
			line.setDirectionRad(startAngleRad + delta * i);
			line.origin = (Point2D.Double) origin.clone();
			res.add(line);
		}
		
		return res;
	}
	
	public Vector<Circle> getCircleDistribution(double granularity){
		Vector<Circle> res = new Vector<Circle>();
		double angleRad = angle * Math.PI / 180;
		double startAngleRad = getStartAngle() * Math.PI / 180;
		
		int m = (int) Math.ceil((radius / (granularity * 0.9))); // <-
		double deltaM = radius / m;
		
		int n;
		double deltaN;
		for (int i = 1; i <= m ; i++){
			n = (int) Math.ceil(angleRad * deltaM * i / granularity); // <-
			deltaN = angleRad / n;
			
			for (int j = 0; j <= n ; j++){
				Circle circle = new Circle(granularity/2);
				circle.setOrigin(origin.x + Math.cos(startAngleRad + deltaN * j) * deltaM * i, origin.y + Math.sin(startAngleRad + deltaN * j) * deltaM * i);
				res.add(circle);
			}
		}
		
		return res;
	}
}
